package com.progark.sound.model;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import com.progark.sound.R;

import android.content.res.Resources;

public class GameModel {

	private Player player1, player2, currentPlayer;
	private int currentScore;
	private ArrayList<Task> listOfTasks, randomisedList;
	private Task currentTask;
	private Resources resources;
	private boolean gameFinished;
	
	public GameModel(Player player1, Player player2, Resources resources){
		this.player1 = player1;
		this.player2 = player2;
		currentPlayer = player1;
		currentScore = 2000; //StartScore, kan endres, bare ett forslag
		listOfTasks = new ArrayList<Task>();
		randomisedList = new ArrayList<Task>();
		this.resources = resources;
		importSounds();
		currentTask = getTask(currentPlayer.getTurnsUsed());
		gameFinished = false;
	}
	
	private void importSounds(){
		InputStreamReader inputReader = new InputStreamReader(resources.openRawResource(R.raw.tasks));
		BufferedReader buffReader = new BufferedReader(inputReader);
		String inputLine;
		try {
			inputLine = buffReader.readLine();
			while(inputLine != null){
				String[] stringList = inputLine.split(":");
				//SYNTAKS: soundfil:hint:alt1:alt2:alt3:alt4
				listOfTasks.add(new Task(stringList[0], stringList[1], stringList[2], stringList[3], stringList[4], stringList[5]));
				inputLine = buffReader.readLine();
			}
			buffReader.close();
			randomizeList();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	public void switchCurrentPlayer(){
		if (currentPlayer == player1)
			currentPlayer = player2;
		else
			currentPlayer = player1;
		updateCurrentTask();
	}
	
	public Player getCurrentPlayer(){
		return currentPlayer;
	}
	public Player getPlayer1(){
		return player1;
	}
	public Player getPlayer2(){
		return player2;
	}
	

	
	
	public int getCurrentScore(){
		return currentScore;
	}
	
	public void hintUsed(){
		currentScore -= 500; //Forslag
		currentPlayer.increaseHintsUsed();
	}
	
	public void correctAnswer(){
		currentPlayer.addToScore(currentScore);
		currentPlayer.increaseTurnsUsed();
		switchCurrentPlayer();
		resetScore();
	}
	
	public void wrongAnswer(){
		currentPlayer.increaseTurnsUsed();
		switchCurrentPlayer();
		resetScore();
	}

	private void resetScore() {
		currentScore = 2000; //Forslag
	}
	

	private void randomizeList() {
		while(!listOfTasks.isEmpty()){
			int i = (int) Math.floor(Math.random()*listOfTasks.size());
			randomisedList.add(listOfTasks.get(i));
			listOfTasks.remove(i);
		}
	}
	
	public ArrayList<Task> getTasks(){
		return randomisedList;
	}
	
	public Task getTask(int i){
		if (i >= 0 && i < randomisedList.size()){
			return randomisedList.get(i);
		}
		else
			return null;
	}
	
	public Task getCurrentTask(){
		return currentTask;
	}
	
	public void updateCurrentTask(){
		if(currentPlayer.getTurnsUsed() < randomisedList.size()){
			currentTask = randomisedList.get(currentPlayer.getTurnsUsed());
			
		} else {
			gameFinished = true;
		}
	}
	
	public boolean isGameFinished(){
		return gameFinished;
	}
	
	
}
